# Copyright 2021 Ant Group Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "gentbl_filegroup", "td_library")
load("//bazel:spu.bzl", "spu_cc_library")

package(
    default_visibility = ["//visibility:public"],
    licenses = ["notice"],
)

td_library(
    name = "pphlo_dialect_td_files",
    srcs = [
        "pphlo_attrs.td",
        "pphlo_base_enums.td",
        "pphlo_dialect.td",
        "pphlo_ops.td",
        "pphlo_types.td",
    ],
    deps = [
        "@llvm-project//mlir:InferTypeOpInterfaceTdFiles",
        "@llvm-project//mlir:OpBaseTdFiles",
        "@llvm-project//mlir:SideEffectInterfacesTdFiles",
    ],
)

gentbl_cc_library(
    name = "pphlo_base_enums_inc_gen",
    tbl_outs = [
        (
            ["-gen-enum-decls"],
            "pphlo_base_enums.h.inc",
        ),
        (
            ["-gen-enum-defs"],
            "pphlo_base_enums.cc.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "pphlo_base_enums.td",
    deps = [":pphlo_dialect_td_files"],
)

gentbl_cc_library(
    name = "pphlo_ops_inc_gen",
    tbl_outs = [
        (
            ["-gen-op-decls"],
            "pphlo_ops.h.inc",
        ),
        (
            ["-gen-op-defs"],
            "pphlo_ops.cc.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "pphlo_ops.td",
    deps = [":pphlo_dialect_td_files"],
)

gentbl_cc_library(
    name = "pphlo_types_inc_gen",
    tbl_outs = [
        (
            ["-gen-typedef-decls"],
            "pphlo_types.h.inc",
        ),
        (
            ["-gen-typedef-defs"],
            "pphlo_types.cc.inc",
        ),
        (
            ["-gen-dialect-decls"],
            "pphlo_dialect.h.inc",
        ),
        (
            ["-gen-dialect-defs"],
            "pphlo_dialect.cc.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "pphlo_types.td",
    deps = [":pphlo_dialect_td_files"],
)

gentbl_cc_library(
    name = "pphlo_attrs_inc_gen",
    tbl_outs = [
        (
            ["-gen-attrdef-decls"],
            "pphlo_attrs.h.inc",
        ),
        (
            ["-gen-attrdef-defs"],
            "pphlo_attrs.cc.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "pphlo_attrs.td",
    deps = [":pphlo_dialect_td_files"],
)

gentbl_cc_library(
    name = "pphlo_ops_pattern_inc_gen",
    tbl_outs = [
        (
            ["-gen-rewriters"],
            "pphlo_patterns.cc.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "pphlo_patterns.td",
    deps = [
        ":pphlo_dialect_td_files",
        "@llvm-project//mlir:FuncTdFiles",
    ],
)

spu_cc_library(
    name = "pphlo_dialect",
    srcs = glob([
        "*.cc",
    ]),
    hdrs = glob([
        "*.h",
    ]),
    deps = [
        ":pphlo_attrs_inc_gen",
        ":pphlo_base_enums_inc_gen",
        ":pphlo_ops_inc_gen",
        ":pphlo_ops_pattern_inc_gen",
        ":pphlo_types_inc_gen",
        "//libspu/core:prelude",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:TensorDialect",
        "@stablehlo//:stablehlo_type_inference",
    ],
)

gentbl_filegroup(
    name = "pphlo_op_doc",
    tbl_outs = [
        (
            ["-gen-op-doc"],
            "pphlo_op_doc.md",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "pphlo_ops.td",
    deps = [":pphlo_dialect_td_files"],
)
